<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <!-- 构造函数封装 -->
    <script>
        /* 
        封装一个作者类Author
        ·属性：姓名，作品集（字符串数组）
        ·方法：创作（抽象方法：一行代码都没有，等子类自己去实现）（产出文艺作品）
        */
        function Author(name, artworks = []) {
            this.name = name
            this.artworks = artworks
        }
        Author.prototype.chuangzuo = function () {
            console.log(`${this.name}正在创作，具体请子类自行实现...`);
        }

        /* 
        封装一个文艺作品类Artwork
        ·属性：名称，作者（Author实例）
        ·方法：发表（发布后该作品自动进入作者的作品集）        
        */
        function Artwork(name, author, type) {
            this.name = name
            this.author = author
            this.type = type
        }
        Artwork.prototype.publish = function () {
            console.log(`${this.name}发表惹`);
            this.author.artworks.push(this.name)
        }

        /* 实现作者子类：导演Director，实现创作方法，产出电影实例 */
        function Director(name, artworks) {
            // this.name = name
            // this.artworks = artworks
            Author.apply(this, [name, artworks])
        }
        // 继承Author的全部方法
        Director.prototype = new Author()

        /* 实现作者子类：画家Painter，实现创作方法，产出画作实例 */
        function Painter(name, artworks) {
            // this.name = name
            // this.artworks = artworks
            Author.apply(this, [name, artworks])
        }
        // 继承Author的全部方法
        Painter.prototype = new Author()

        /* 实现文艺作品子类：电影Movie，作者是导演实例 */
        function Movie(name, director = null) {
            Artwork.call(this, name, director, "movie")
        }
        Movie.prototype = new Artwork()

        /* 实现文艺作品子类：画作Picture，作者是画家实例 */
        function Picture(name, painter = null) {
            Artwork.call(this, name, painter, "picture")
        }
        Picture.prototype = new Artwork()
    </script>

    <!-- 业务逻辑 -->
    <script>
        /* 输入姓名、类别(导演/画家)，创建出一个导演/画家实例； */
        function createAuthor(name, type) {
            switch (type) {
                case "director":
                    return new Director(name)
                case "painter":
                    return new Painter(name)
                default:
                    return null;
            }
        }

        /* 根据输入的名称、类别、作者，生成一个文艺作品实例 */
        function createArtwork(name, type, author) {
            let artwork = null
            switch (type) {
                case "movie":
                    artwork = new Movie(name, author)
                    break
                case "picture":
                    artwork = new Picture(name, author)
                    break
                default:
                    return null;
            }
            artwork && artwork.publish()

            return artwork
        }

        /* 
        查看作品，显示全部文艺作品 
        按类别查看、按作者查看、按照作品类型查看
        filter = {authorName:"陈凯歌",artworkType="movie"}
        */
        // 新中国文艺作品集
        const artworks = []
        function getArtworks(filter = null) {
            let retArr = artworks;
            if (!filter) {
                //什么也不干
            } else {
                // 过滤出特定作者的作品
                if (filter.authorName) {
                    retArr = retArr.filter(aw => aw.author.name === filter.authorName)
                }

                // 过滤出特定类型的作品
                if (filter.artworkType) {
                    retArr = retArr.filter(aw => aw.type === filter.artworkType)
                }
            }

            return retArr
        }

        /* 展示作品 */
        function showArtworks(artworks, useJson = false) {
            artworks = useJson ? JSON.stringify(artworks) : artworks
            console.log(artworks);
        }

        const gqj = createAuthor("宫崎骏", "director")
        const dfq = createAuthor("达芬奇", "painter")

        /* 新中国文艺作品集 */
        artworks.push(createArtwork("天空之城", "movie", gqj))
        artworks.push(createArtwork("千与千寻", "movie", gqj))
        artworks.push(createArtwork("蒙娜丽莎", "picture", dfq))
        artworks.push(createArtwork("最后的晚餐", "picture", dfq))
        artworks.push(createArtwork("牛人某芬奇", "movie", dfq))

        showArtworks(
            getArtworks(
                {
                    authorName:"达芬奇",
                    artworkType:"movie"
                }
            ),
            true
        )

    </script>
</body>

</html>